package com.xuanxu.blogs.module.article.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuanxu.blogs.module.article.entity.dao.ArticleDao;
import com.xuanxu.blogs.module.article.entity.vo.admin.ArticleResponseVo;
import com.xuanxu.blogs.module.article.entity.vo.admin.SelectArticleRequestVo;
import com.xuanxu.blogs.module.article.entity.vo.admin.UpdateArticleRequestVo;
import com.xuanxu.blogs.module.article.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author XuanXu
 * @version 1.0
 * @date 2024-08-23 13:24
 */
@RestController
@RequestMapping("/admin-api/article")
@RequiredArgsConstructor
@Api(tags = "文章相关接口")
@Slf4j
public class AdminArticleController {

    private final ArticleService articleService;

    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "currentPage", value = "页码", required = true),
            @ApiImplicitParam(dataType = "Integer", name = "pageSize", value = "页面大小", required = true),
    })
    @GetMapping("/page/{currentPage}/{pageSize}")
    @ApiOperation("分页查询文章")
    public ResponseEntity<IPage<ArticleResponseVo>> pageSelectArticle(@PathVariable("currentPage") Long currentPage,
                                                                      @PathVariable("pageSize") Long pageSize,
                                                                      SelectArticleRequestVo selectArticleRequestVo) {

        ArticleDao articleDao = BeanUtil.copyProperties(selectArticleRequestVo, ArticleDao.class);

        IPage<ArticleDao> articleDaoPage = articleService.pageSelectArticle(currentPage, pageSize, articleDao);
        IPage<ArticleResponseVo> articleResponseVoPage = new Page<>();
        BeanUtil.copyProperties(articleDaoPage, articleResponseVoPage);

        if (articleDaoPage != null) {
            List<ArticleResponseVo> articleResponseVoList = BeanUtil.copyToList(articleDaoPage.getRecords(), ArticleResponseVo.class);
            articleResponseVoPage.setRecords(articleResponseVoList);
        }

        return ResponseEntity.ok(articleResponseVoPage);
    }

    @PutMapping()
    @ApiOperation("修改文章")
    public ResponseEntity<Boolean> updateArticleByArticleId(@RequestBody UpdateArticleRequestVo updateArticleRequestVo) {
        ArticleDao articleDao = BeanUtil.copyProperties(updateArticleRequestVo, ArticleDao.class);
        boolean isSuccess = articleService.updateArticleByArticleId(articleDao);
        return ResponseEntity.ok(isSuccess);
    }
}
